package com.xunwulian.weixin.h5.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xunwulian.api.iface.SubscriberAPI;
import com.xunwulian.api.iface.UserAPI;
import com.xunwulian.base.constant.ConstantBean;
import com.xunwulian.base.entity.User;
import com.xunwulian.weixin.h5.constant.ResultType;
//import org.apache.shiro.crypto.hash.SimpleHash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 会员-接口类
 * 相关参数协议： 00 请求失败 01 请求成功 02 返回空值 03 请求协议参数不完整 04 用户名或密码错误 05 FKEY验证失败
 */
@RestController
@RequestMapping(value = "/appuser")
public class IntAppuserController {

    private static final Logger logger = LoggerFactory.getLogger(IntAppuserController.class);

    @Reference
    private SubscriberAPI subscriberAPI;
    @Reference
    private UserAPI userAPI;

    /**
     * APP用户登录
     */
    @Deprecated
    @RequestMapping(value = "/login")
    @ResponseBody
    public Object loginAppuser(@RequestParam("userName") String userName,
                               @RequestParam("password") String password) {

        logger.info("APP用户登录");

        Map<String, Object> map = new HashMap();
        logger.info("userName:" + userName + ", password:" + password);

//        String passwdEncrypt = new SimpleHash("SHA-1", userName, password).toString(); // 密码加密

//        logger.info("userName:" + userName + ", passwdEncrypt:" + passwdEncrypt);
//
//        User user = userAPI.getUserByNameAndPwd(userName, passwdEncrypt);
//        if (user == null) {
//            map.put("result", ResultType.USER_PASSWD_ERROR.getType()); // 用户名或者密码错
//            logger.info("User is null!");
//        } else {
//            map.put("result", ResultType.SUCCESS.getType());
//        }
//
//        map.put("user", user);
        return map;
    }


    @RequestMapping(value = "/bind")
    @ResponseBody
    public Object bind(@RequestParam("userName") String userName,
                       @RequestParam("openId") String openId,
                       @RequestParam("password") String password) {

        logger.info("bind, userName:" + userName + ", openId:" + openId + ", password:" + password);
        Map<String, Object> map = new HashMap();

        // TODO 考虑找不到用户的情况
        User user = userAPI.getByName(userName);
        if (user != null) {
            String userId = user.getUSER_ID();

            subscriberAPI.bind(userId, openId);
            map.put("result", ResultType.SUCCESS.getType());
            map.put("userId", userId);
        } else {
            map.put("result", ResultType.FAILED.getType());
        }

        return map;
    }


    @RequestMapping(value = "/unbind")
    @ResponseBody
    public Object unbind(@RequestParam("openId") String openId) {

        logger.info("unbind, openId:" + openId);

        subscriberAPI.unbind(openId);

        Map<String, Object> map = new HashMap();
        map.put("userId", ConstantBean.TEST_USER_ID);
        map.put("userName", ConstantBean.TEST_USER_NAME);
        map.put("result", ResultType.SUCCESS.getType());

        return map;
    }

}

